package com.ymatou.iapi.prodstock.testcase;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prodstock.parameter.SendActivityProductStartOrEndBean;
import com.ymatou.iapi.prodstock.service.SendActivityProductStartOrEndCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
/**
*发送活动库存开始结束总线消息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendActivityProductStartOrEnd{
	private static SendActivityProductStartOrEndBean sendactivityproductstartorendBean;
	private static SendActivityProductStartOrEndCall sendactivityproductstartorendCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送活动库存开始结束总线消息");
	}

	@Before
	public void caseUp() {
		sendactivityproductstartorendBean=new SendActivityProductStartOrEndBean();
		sendactivityproductstartorendCall=new SendActivityProductStartOrEndCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendactivityproductstartorendCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendActivityProductStartOrEnd_001() {
		Logger.start(true,"发送活动库存开始结束总线消息成功");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			sendactivityproductstartorendBean.setActivityId(activityId);
			sendactivityproductstartorendBean.setProductId(productId);
			sendactivityproductstartorendBean.setActivityStatus(9);
			sendactivityproductstartorendCall.setData(sendactivityproductstartorendBean);
			sendactivityproductstartorendCall.callService();		
			Logger.verifyEquals("ok", sendactivityproductstartorendCall.getReturnData(), "验证返回");
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initActivityStatusChange(productId);
			List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(1, joa.size(), "验证发送MQ-activity_status_change");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}